# -*- coding: utf-8 -*-
# @Author : chenliang
# @Time : 2025/7/3 09:47

import requests
from Common.get_sql_object import GetSqlObject
from Common.business_common_tool import BusinessCommonTools
from Common.excel import excel
import os


excelpath = input('目标excel文件地址:')
# folderpath = input('所有附件地址：')
# oo = excel("/Users/chenliang/Documents/浩鲸/OA/历史表单迁移/DingTalk Form Extract/HR HERE_2023.xlsx")
#oo = excel("/Users/chenliang/Desktop/司机信息.xlsx")
oo = excel(excelpath)


# 获取当前工作目录
current_directory = os.getcwd()
# 构造目标目录路径
target_dir = os.path.join(current_directory, "司机协议")  # 推荐使用 os.path.join 避免路径分隔符问题
# 创建目录
os.makedirs(target_dir, exist_ok=True)
# 输出路径（此时 target_dir 就是你要的路径）
print(f"成功创建或确认目录存在: {target_dir}")





result=oo.getExcelData('司机单号','合同号')


print(result)

for i in result[0]:
    print(i)
    sql = """SELECT
        a.agreement,
        c.NAME,
        d.license_code
    FROM
        agreement_ref a,
        d_driver_order b,
        d_driver c,
        d_car_manage_info d
    WHERE
        a.order_code = '@a'
        AND a.order_code = b.order_code
        AND b.accepting_id = c.id
        AND d.user_id = c.id"""
    mysql_source = GetSqlObject.get_instance("PRO")
    driver_sgreement = BusinessCommonTools().get_driver_agreement('PRO', i[0], sql, mysql_source)

    url_download = driver_sgreement[0]['agreement']
    response_down = requests.get(url=url_download)

    # 检查响应状态码
    if response_down.status_code == 200:
        save_path = target_dir + '/' + driver_sgreement[0]['NAME'] + '_' + driver_sgreement[0]['license_code']  + '_' + i[0] + '.pdf'

        with open(save_path, 'wb') as f:
            for chunk in response_down.iter_content(chunk_size=8192):
                if chunk:
                    f.write(chunk)
        print(f"文件已成功保存至：{save_path}")
    else:
        print(f"请求失败，状态码: {response_down.status_code}")
        print("响应内容:", response_down.text)



